﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money.Interface
{
    public partial class AccountForm : Form
    {
        Account _account;

        private decimal _transactionBalance;

        public AccountForm(Account account, IEnumerable<string> parsers, List<string> fileNameTemplates)
        {
            InitializeComponent();

            foreach (string parser in parsers)
            {
                _importParser.Items.Add(parser);
            }

            foreach(string fileNameTemplate in fileNameTemplates)
            {
                _filenameTemplate.Items.Add(fileNameTemplate);
            }

            _account = account;

            _accountName.Text = _account.Name;
            _importParser.Text = _account.Parser;
            _filenameTemplate.Text = _account.FilenameTemplate;
            _openingBalance.Text = _account.OpeningBalance.ToString("F2");
            _importPath.Directory = _account.StatementPath;

            // calculate the balance for the account
            _transactionBalance = _account.TransactionBalance;

            _balance.Text = (_transactionBalance + _account.OpeningBalance).ToString("F2");
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            _account.Name = _accountName.Text;
            _account.Parser = _importParser.Text;
            _account.StatementPath = _importPath.Directory;
            _account.FilenameTemplate = _filenameTemplate.Text;
            try
            {
                _account.OpeningBalance = Convert.ToDecimal(_openingBalance.Text);
            }
            catch
            {
                _account.OpeningBalance = 0;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                decimal newBalance = Convert.ToDecimal(_newBalance.Text);
                decimal currentBalance = Convert.ToDecimal(_balance.Text);

                decimal originalBalance = newBalance - _transactionBalance;

                _openingBalance.Text = originalBalance.ToString("F2");
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
    }
}
